﻿using Dapper;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class Hepler
    {
        /// <summary>
        /// 连接数据库字符串
        /// </summary>
        private static readonly string sqlconnection = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;

        /// <summary>
        /// 获取Sql Server的连接数据库对象:SqlConnection
        /// </summary>
        /// <returns></returns>
        public static SqlConnection OpenConnection()
        {
            SqlConnection connection = new SqlConnection(sqlconnection);
            MySql.Data.MySqlClient.MySqlConnection mySqlConnection = new MySql.Data.MySqlClient.MySqlConnection(sqlconnection);
            connection.Open();
            return connection;
        }

        public static MySqlConnection MySqlOpenConnection()
        {
            MySql.Data.MySqlClient.MySqlConnection mySqlConnection = new MySql.Data.MySqlClient.MySqlConnection(sqlconnection);
            mySqlConnection.Open();
            return mySqlConnection;
        }

        /// <summary>
        /// 获取总的行数
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public int GetAllRowNum(string tableName)
        {
            using (IDbConnection conn = Hepler.MySqlOpenConnection())
            {
                string query = "select count(*) from " + tableName;
                return Convert.ToInt32(SqlMapper.ExecuteScalar(conn, query));
            }
        }

        /// <summary>
        /// 获取总的页数
        /// </summary>
        /// <param name="pageNum">每一页的数量（多少行）</param>
        /// <param name="tableName">表名</param>
        /// <returns></returns>
        public int GetAllPageNum(int pageNum,string tableName)
        {
            return (int)Math.Floor(1.0 * GetAllRowNum(tableName) / pageNum);
        }
    }
}
